//
//  MoreimageDetailsViewController.m
//  news
//
//  Created by lanou on 15/12/18.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "MoreimageDetailsViewController.h"
#import "MoreImage.h"

@interface MoreimageDetailsViewController ()<UIScrollViewDelegate>

@property (nonatomic, retain) NSMutableArray *photosArray;

@property (nonatomic, strong) UIScrollView *photosScrollView;

@property (nonatomic, strong) UITextView *textView;

@property (nonatomic, strong) UILabel *indexLabel;

@end

@implementation MoreimageDetailsViewController

- (NSMutableArray *)photosArray
{
    if (!_photosArray) {
        _photosArray = [[NSMutableArray alloc] init];
    }
    return _photosArray;
}


#pragma mark -
#pragma mark ---网络解析---

- (void)josnDetails
{
    NSString *str = [self.photosetID substringFromIndex:4];
    
    NSArray *urlArray = [str componentsSeparatedByString:@"|"];
    NSString *url = [NSString stringWithFormat:@"http://c.m.163.com/photo/api/set/%@/%@.json",urlArray[0], urlArray[1]];
    [LORequestManger GET:url success:^(id response) {
        
        NSDictionary *dic = (NSDictionary *)response;
        
        for (NSDictionary *photos in dic[@"photos"]) {
            MoreImage *moreImage = [[MoreImage alloc] init];
            [moreImage setValuesForKeysWithDictionary:photos];
            [self.photosArray addObject:moreImage];
        }
        
        self.photosScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, -64, kWidth, kHeight)];
        
        self.photosScrollView.contentSize = CGSizeMake(kWidth * self.photosArray.count, kHeight);
        self.photosScrollView.pagingEnabled = YES;
        
        self.photosScrollView.delegate = self;
        
        self.photosScrollView.showsHorizontalScrollIndicator = NO;
        
        for (int i = 0; i < self.photosArray.count; i ++) {
            
            [self jiazai:i];

        }
        
        [self.view addSubview:self.photosScrollView];
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, kHeight - 200, kWidth, 90)];
        view.backgroundColor = [UIColor grayColor];
        view.alpha = 0.3;
        [self.view addSubview:view];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, kHeight - 200, kWidth - 60, 30)];
        titleLabel.text = self.titleText;
        [self setLabel:titleLabel textColor:[UIColor whiteColor] font:[UIFont boldSystemFontOfSize:16.0] backgroundColor:[UIColor clearColor]];
        [self.view addSubview:titleLabel];
        
        self.indexLabel = [[UILabel alloc] initWithFrame:CGRectMake(kWidth - 60, kHeight - 200, 30, 30)];
        [self setLabel:self.indexLabel textColor:[UIColor whiteColor] font:[UIFont boldSystemFontOfSize:16.0] backgroundColor:[UIColor clearColor]];
        self.indexLabel.textAlignment = NSTextAlignmentRight;
        self.indexLabel.text = @"1";
        
        [self.view addSubview:self.indexLabel];
        
        UILabel *aLabel = [[UILabel alloc] initWithFrame:CGRectMake(kWidth - 30, kHeight - 200, 30, 30)];
        [self setLabel:aLabel textColor:[UIColor whiteColor] font:[UIFont boldSystemFontOfSize:10.0] backgroundColor:[UIColor clearColor]];
        aLabel.text = [NSString stringWithFormat:@"/%ld",self.photosArray.count];
        [self.view addSubview:aLabel];
        
        MoreImage *moreImage = self.photosArray[0];
        
        self.textView = [[UITextView alloc] initWithFrame:CGRectMake(5, kHeight - 170, kWidth - 10, 60)];
        self.textView.text = moreImage.note;
        self.textView.backgroundColor = [UIColor clearColor];
        self.textView.editable = NO;
        self.textView.textColor = [UIColor whiteColor];
        [self.view addSubview:self.textView];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
    
    
}

#pragma mark -
#pragma mark ---设置label属性---

- (void)setLabel:(UILabel *)label textColor:(UIColor *)color font:(UIFont *)font backgroundColor:(UIColor *)bColor
{
    label.textColor = color;
    label.font = font;
    label.backgroundColor = bColor;
}

#pragma mark -
#pragma mark ---scrollView代理方法---

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x / kWidth;
    MoreImage *moreImage = self.photosArray[index];
    self.textView.text = moreImage.note;
    
    self.indexLabel.text = [NSString stringWithFormat:@"%ld",index + 1];
    
}

#pragma mark -
#pragma mark ---加载图片---

- (void)jiazai:(int)index;
{
    MoreImage *moreImage = self.photosArray[index];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kWidth)];
    
    imageView.center = CGPointMake(kWidth / 2 + kWidth * index, kHeight / 2);
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(kWidth * index, 0, kWidth, kHeight)];
    
    LoadingAnimationView *loadingView = [[LoadingAnimationView alloc] init];
    [loadingView beginLoadingWithView:view];
    
    imageView.backgroundColor = [UIColor whiteColor];
    
    [self.photosScrollView addSubview:imageView];
    
    [self.photosScrollView addSubview:view];
    
    [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:moreImage.imgurl]] queue:([NSOperationQueue mainQueue]) completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        UIImage *image = [UIImage imageWithData:data];
        CGFloat ratio = image.size.height / image.size.width;
        if (!image) {
            NSLog(@"NAN");
        } else {
            imageView.bounds = CGRectMake(0, 0, kWidth, kWidth * ratio);
            //    imageView.center = CGPointMake(kWidth / 2 + kWidth * i, kHeight / 2);
            imageView.image = image;
            
        }
        
        [view removeFromSuperview];
        [loadingView endLoading];
    }];
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self josnDetails];
    
    self.view.backgroundColor = [UIColor blackColor];
    
    self.navigationController.navigationBar.translucent = NO;
    
    UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:(UIBarButtonItemStylePlain) target:self action:@selector(back)];
    self.navigationItem.leftBarButtonItem = left;
    
    // Do any additional setup after loading the view.
}

- (void)back
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
